package persistency.reader;

/**
 * StudentReader.java
 * Purpose: this class represents a course that students can take
 * 
 * @author Hans Brems
 * @version 1.0 05/11/2011
 */

import java.util.ArrayList;

import model.FreeStudent;
import model.RetrainingStudent;
import model.Student;

import persistency.reader.FreeStudentReader;

public class StudentReader extends ContentReader {

	FreeStudentReader freeStudentReader;
	RetrainingStudentReader retrainingStudentReader;
	
	/** Constructs a course reader object **/
	public StudentReader() {
		this.freeStudentReader = new FreeStudentReader();
		this.retrainingStudentReader = new RetrainingStudentReader();
	}
	
	/** Constructs a student reader object */
	public StudentReader(String studentFilePath, String freeStudentFilePath, String retrainingStudentFilePath) {
		super(studentFilePath);
		this.freeStudentReader = new FreeStudentReader(freeStudentFilePath);
		this.retrainingStudentReader = new RetrainingStudentReader(retrainingStudentFilePath);
	}
	
	/** Returns the path of the free student data file */
	public String getFreeStudentFilePath() {
		return this.freeStudentReader.getFilePath();
	}
	
	/** Sets the path of the free student data file */
	public void setFreeStudentFilePath(String filePath) {
		this.freeStudentReader.setFilePath(filePath);
	}
	
	/** Returns the path of the free student data file */
	public String getRetrainingStudentFilePath() {
		return this.retrainingStudentReader.getFilePath();
	}
	
	/** Sets the path of the free student data file */
	public void setRetrainingStudentFilePath(String filePath) {
		this.retrainingStudentReader.setFilePath(filePath);
	}
	
	/** Reads a text file containing student objects into a collection */
	public ArrayList<Student> read() {
		
		// Create list
		ArrayList<Student> students = new ArrayList<Student>();
		students.addAll(this.readFreeStudents());
		students.addAll(this.readRetrainingStudents());
		
		// Skip the first line (header)
		this.scanner.nextLine();

		// Construct a course object from each line
		while (this.scanner.hasNext()) {
			
			// Get student number
			String studentNumber = this.scanner.next();
			
			// Find student in array lists
			for (Student student : students) {
				
				// Set student name
				if (student.getStudentNumber().equals(studentNumber)) {
					student.setName(this.scanner.next());
					break;
				}
			}
		}
		
		// Return list
		return students;
	}
	
	/* Read free student objects from text file */
	private ArrayList<FreeStudent> readFreeStudents() {
		
		// Create list
		ArrayList<FreeStudent> freeStudents = new ArrayList<FreeStudent>();
		
		try
		{
			// Open the text file
			this.freeStudentReader.open();

			// Fill list
			freeStudents = this.freeStudentReader.read();
			
			// Close the text file
			this.freeStudentReader.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		
		// Return the results
		return freeStudents;
	}
	
	/* Read retraining student objects from text file */
	private ArrayList<RetrainingStudent> readRetrainingStudents() {
		
		// Create list
		ArrayList<RetrainingStudent> retrainingStudents = new ArrayList<RetrainingStudent>();
		
		try
		{	
			// Open the text file
			this.retrainingStudentReader.open();

			// Fill list
			retrainingStudents = this.retrainingStudentReader.read();
			
			// Close the text file
			this.retrainingStudentReader.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		
		// Return results
		return retrainingStudents;
	}
}